package kk.controller;

import com.github.pagehelper.PageInfo;
import kk.domain.Orders;
import kk.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;

@RequestMapping("/orders")
@Controller
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findAll(Integer pageNum,Integer pageSize) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        List<Orders> ordersList = ordersService.findAll(pageNum, pageSize);
        PageInfo pageInfo=new PageInfo(ordersList);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("orders-list");
        return modelAndView;
    }

    /**
     * 查询一个订单的详细信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findById(Integer id)throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        Orders orders = ordersService.findById(id);
        modelAndView.addObject("orders",orders);
        modelAndView.setViewName("orders-show");
        return modelAndView;
    }

    /**
     * 查询一个订单的简单信息，从而进行订单信息的修改
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/findOneOrder")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findOneOrder(Integer id)throws Exception{
        Orders orders = ordersService.findOneOrder(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("orders",orders);
        modelAndView.setViewName("orders-edit");
        return modelAndView;
    }

    @RequestMapping("/deleteById")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String deleteById(@RequestParam("id") Integer oid) throws Exception{
        ordersService.deleteById(oid);
        return "redirect:/orders/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/deleteByIds")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String deleteByIds(int[] ids) throws Exception{
        for (int oid : ids) {
            ordersService.deleteById(oid);
        }
        return "redirect:/orders/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String update(Orders orders) throws Exception{
        ordersService.update(orders);
        return "redirect:/orders/findAll?pageNum=1&pageSize=4";
    }


    @RequestMapping("/add")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String add(String orderNum, @RequestParam("orderTime") Date orderTime,Integer peopleCount,String orderDesc,Integer payType,Integer orderStatus,
                      Integer productId,
                      String member_name,String member_nickname,String member_phoneNum,String member_email,
                      String traveller_name,String traveller_sex,String traveller_phoneNum,Integer credentialsType,String credentialsNum,Integer travellerType) throws Exception{
        ordersService.add(orderNum,orderTime,peopleCount,orderDesc,payType,orderStatus,
            productId,
            member_name,member_nickname,member_phoneNum,member_email,
            traveller_name,traveller_sex,traveller_phoneNum,credentialsType,credentialsNum,travellerType
        );
        return "redirect:/orders/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/findByOrderNum")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findByOrderNum(String orderNum,Integer pageNum,Integer pageSize){
        String newOrderNum="%"+orderNum+"%";
        List<Orders> ordersList = ordersService.findByOrderNum(newOrderNum, pageNum, pageSize);
        PageInfo pageInfo=new PageInfo(ordersList);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("orderNum",orderNum);
        modelAndView.setViewName("orders-list");
        return modelAndView;
    }
}
